package lizhe;


/**
 * 旋转图像
 * 给定一个 n×n 的二维矩阵matrix 表示一个图像。请你将图像顺时针旋转 90 度。
 * <p>
 * 你必须在 原地 旋转图像，这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。
 */
public class Rotate2 {

    public static void rotate(int[][] matrix) {
        int length = matrix.length;
        int[] tmp = new int[length];
        for (int i = 0; i < length / 2; i++) {
            int symmetryIndex = length - 1 - i;
            for (int j = 0; j < length; j++) {
                tmp[j] = matrix[symmetryIndex][j];
            }
            for (int j = 0; j < length; j++) {
                matrix[symmetryIndex][j] = matrix[i][j];
                matrix[i][j] = tmp[j];
            }
        }

        int tmpVal;
        for (int i = 0; i < length; i++) {
            for (int j = i; j < length; j++) {
                tmpVal = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = tmpVal;
            }
        }
    }

    public static void rotate2(int[][] matrix) {
        int length = matrix.length;
        for (int i = 0; i < length / 2; i++) {
            for (int j = i; j < length - i - 1; j++) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[length - j - 1][i];
                matrix[length - j - 1][i] = matrix[length - i - 1][length - j - 1];
                matrix[length - i - 1][length - j - 1] = matrix[j][length - i - 1];
                matrix[j][length - i - 1] = tmp;
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = {{5, 1, 9, 11}, {2, 4, 8, 10}, {13, 3, 6, 7}, {15, 14, 12, 16}};
        rotate(matrix);
        System.out.println("");
    }
}
